class GradesController < ApplicationController
  # GET /grades
  # GET /grades.xml
  # Get grades of current user
  # url: 10.10.10.154:3000/grades
  def index
    @selected_grades = current_user ? current_user.grades : []
    puts "selected_grades: #{@selected_grades.inspect}"
    @grades = Grade.all.map do |grade|
      {:id => grade.id, :name => grade.name, :selected => @selected_grades.any?{|g| g.id == grade.id}}
    end
    puts "grades: #{@grades.inspect}"
    
    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @grades }
      format.xml  { render :xml => @grades }
    end
  end
  
  # POST /grades.json
  # url: 10.10.10.154:3000/grades.json  
  # params structure
  # grade[name] = ''
  # grade[description] = ''  
  def create
    puts "params: #{params.inspect}"
    @grade = Grade.new(params[:grade])

    respond_to do |format|
      if @grade.save
        format.html # index.html.erb
        format.json  { render :json => @grades }
        format.xml  { render :xml => @grades }
      else
        format.html # index.html.erb
        format.json  { render :json => {:error_msg => 'no grade'}}        
      end
      
    end
  end

  # GET /grades/1
  # GET /grades/1.xml
  # GET /grades/1.json
  def show
    @grades = Grade.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @grades }
      format.json  { render :json => @grades }
    end
  end
end
